args <- commandArgs(TRUE)
setwd(args[1])
expr<-read.table("temp_input",sep="\t",head=T)
ref<-read.table("/data1/bsi/BORA_processing/devel/eqtl/parallelize_genotypingsurvival/result_ref_file",sep="\t")
rownames(ref)<-ref[,1]
expr1<-merge(ref,expr,by="row.names",all.x=F)
geno<-read.table("/data1/bsi/BORA_processing/devel/eqtl/parallelize_genotypingsurvival/input_nohead.tped",sep=" ")
rownames(geno)<-geno[,2]
nc1<- ncol(expr1)
i<-1
k1<-c("gene","snp","Intercept_Estimate","snp_Estimate","Intercept_Std.Error","snp_Std.Error","Intercept_t value","snp_t value","Intercept_Pr(>|t|)","snp_Pr(>|t|)")
while(i<nrow(expr1)+1)
#while(i<2)
{
	l1<-expr1[i,4]-20000
	l2<-expr1[i,4]+20000
	l3<-expr1[i,3]
	index<-c()
	index<-which(geno[,4]>=l1 & geno[,4]<l2  & geno[,1] == l3)
	if(length(index) > 0)
	{
		geno1<-geno[index,]
		nc2 <- ncol(geno1)
		z<-1
		while(z<nrow(geno1)+1)
		{
			g2<-t(as.matrix(geno1[z,5:nc2]))
			g1<-t(as.matrix(expr1[i,6:nc1]))
			d1 <- data.frame(g2,g1)
			glm1 <- glm(g1 ~ g2, data=d1,family=gaussian(link="log"))
			kk<-c( expr1[i,2],as.vector(geno1[z,2]),as.vector(coefficients(summary(glm1))))
			k1<-rbind(k1,kk)
			z<-z+1
		}
	}
	print(i)
	i<-i+1
}
write.table(k1,"eqtl_output",quote=FALSE,sep="\t",col.names=FALSE,row.names=FALSE)
